package cn.iocoder.yudao.module.biz.service.risk.riskPointType;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskPointType.vo.BizRiskPointTypeCreateReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskPointType.vo.BizRiskPointTypeExportReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskPointType.vo.BizRiskPointTypePageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.risk.riskPointType.vo.BizRiskPointTypeUpdateReqVO;
import cn.iocoder.yudao.module.biz.convert.risk.riskPointType.BizRiskPointTypeConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.riskPointType.BizRiskPointTypeDO;
import cn.iocoder.yudao.module.biz.dal.mysql.risk.riskPointType.BizRiskPointTypeMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 风险点类型 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BizRiskPointTypeServiceImpl implements BizRiskPointTypeService {

    @Resource
    private BizRiskPointTypeMapper riskPointTypeMapper;

    @Override
    public String createRiskPointType(BizRiskPointTypeCreateReqVO createReqVO) {
        // 插入
        BizRiskPointTypeDO riskPointType = BizRiskPointTypeConvert.INSTANCE.convert(createReqVO);
        riskPointType.setDeptId(String.valueOf(SecurityFrameworkUtils.getLoginUser().getDeptId()));
        riskPointTypeMapper.insert(riskPointType);
        // 返回
        return riskPointType.getId();
    }

    @Override
    public void updateRiskPointType(BizRiskPointTypeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateRiskPointTypeExists(updateReqVO.getId());
        // 更新
        BizRiskPointTypeDO updateObj = BizRiskPointTypeConvert.INSTANCE.convert(updateReqVO);
        riskPointTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteRiskPointType(String id) {
        // 校验存在
        this.validateRiskPointTypeExists(id);
        // 删除
        riskPointTypeMapper.deleteById(id);
    }

    private void validateRiskPointTypeExists(String id) {
        if (riskPointTypeMapper.selectById(id) == null) {
            throw exception(RISK_POINT_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public BizRiskPointTypeDO getRiskPointType(String id) {
        return riskPointTypeMapper.selectById(id);
    }

    @Override
    public List<BizRiskPointTypeDO> getRiskPointTypeList(Collection<String> ids) {
        return riskPointTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BizRiskPointTypeDO> getRiskPointTypePage(BizRiskPointTypePageReqVO pageReqVO) {
        return riskPointTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BizRiskPointTypeDO> getRiskPointTypeList(BizRiskPointTypeExportReqVO exportReqVO) {
        return riskPointTypeMapper.selectList(exportReqVO);
    }

    @Override
    public List<BizRiskPointTypeDO> getAllPointType() {
        return riskPointTypeMapper.selectList(new BizRiskPointTypeExportReqVO());
    }

}
